package server;

import java.rmi.Remote;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import common.Server;

public class ServerMain {

	//private static final int PORT = 5235;

	public static void main(String[] args) {

		int regPort = Integer.valueOf(args[0]);
		String sequencerIpAddr = args[1];
		

		try {

			ServerImpl serverImpl = new ServerImpl();
			Runnable serverMulticastThread = new ServerMulticastThread(serverImpl);
			new Thread(serverMulticastThread).start();
			
			LocateRegistry.createRegistry(regPort);
			Registry registry = LocateRegistry.getRegistry(regPort);
			Remote remoteObj = UnicastRemoteObject.exportObject(serverImpl, 0);

			String name1 = "server";
			registry.rebind(name1, (Server)remoteObj);
			System.out.println("server bound on port "+regPort);

			//String name2 = "server_update";
			//registry.rebind(name2, (ServerUpdate)remoteObj);
			//System.out.println("server_update bound on port "+regPort);

			serverImpl.init(regPort, sequencerIpAddr);
			
		} catch (Exception e) {
			System.err.println("Server exception:");
			e.printStackTrace();
		}
	}

}
